package info.xiao.dataStructrue.array;

/**
 * @Classname MoveZero
 * @Description 移动零
 * @Href https://leetcode.cn/problems/move-zeroes/
 * @Date 2023/3/13 10:32
 * @Created by 晓龙Oba
 */
public class MoveZero {
    public static void main(String[] args) {
        int[] nums = {0, 1, 0, 3, 12};
        nums = moveZeros(nums);
        System.out.println(nums);
    }

    private static int[] moveZeros(int[] nums) {
        // 选择零进行冒泡
        for (int i = nums.length - 1; i >= 0; i--) {
            if (nums[i] == 0) {
                for (int j = i; j < nums.length - 1; j++) {
                    if (nums[j + 1] != 0) {
                        nums[j] = nums[j] + nums[j + 1];
                        nums[j + 1] = nums[j] - nums[j + 1];
                        nums[j] = nums[j] - nums[j + 1];
                    }
                }
            }
        }
        return nums;
    }
}
